Bayesian Networks Example

This is a simple example of Bayesian Networks using Python and the pgmpy library.

Bayesian Networks Overview

Bayesian Networks are graphical models that represent the probabilistic dependencies among a set of random variables. The structure of the network is defined by a directed acyclic graph (DAG), where nodes represent variables and edges represent probabilistic dependencies. Each node in the network is associated with a conditional probability distribution that models the probability of the variable given its parents in the graph.

Key concepts of Bayesian Networks:

Bayesian Networks are widely used in various domains, including healthcare, finance, and artificial intelligence, for modeling and reasoning under uncertainty.

Python Source Code:

# Import necessary libraries
from pgmpy.models import BayesianModel
from pgmpy.estimators import ParameterEstimator
from pgmpy.inference import VariableElimination
import networkx as nx
import matplotlib.pyplot as plt

# Define the structure of the Bayesian Network
model_structure = [('D', 'G'), ('I', 'G'), ('G', 'L'), ('I', 'S')]

# Create a Bayesian Model
bayesian_model = BayesianModel(model_structure)

# Visualize the Bayesian Network
pos = nx.spring_layout(bayesian_model)
nx.draw(bayesian_model, pos, with_labels=True, font_weight='bold', node_size=700, node_color='skyblue', font_size=10, font_color='black', arrowsize=20)
plt.title('Bayesian Network Structure')
plt.show()

# Generate synthetic data based on the model
data = bayesian_model.sample(n=1000, random_state=42)

# Print a few rows of the generated data
print("Generated Data:")
print(data.head())

# Estimate the parameters of the model from the data
param_estimator = ParameterEstimator(bayesian_model, data)
cpd_estimate = param_estimator.estimate_cpd()

# Print the estimated Conditional Probability Distributions (CPDs)
print("\nEstimated CPDs:")
for cpd in cpd_estimate:
    print(cpd)

# Perform Bayesian Inference
inference = VariableElimination(bayesian_model)
inferred_distribution = inference.query(variables=['G'], evidence={'I': 1, 'D': 0})

# Print the inferred distribution of 'G' given evidence
print("\nInferred Distribution of 'G' given evidence:")
print(inferred_distribution)

Explanation: